﻿using Xejen.Communication.Http.Communicator;
using Xejen.Communication.Soap.Communicator;
using Xejen.Communication.Soap.Configuration;
using Xejen.Hosting;
using Xejen.Hosting.Extensions;
using Xejen.Logger;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xejen.Communication.Soap.Extensions
{
    /// <summary>
    /// <inheritdoc cref="ISoapCommunicatorBuilder"/>的实现
    /// </summary>
    internal sealed class SoapCommunicatorBuilder : CommunicatorBuilder<ISoapCommunicatorBuilder>, ISoapCommunicatorBuilder
    {
        private readonly ILogger _logger;
        private readonly SoapConfig _soapConfig = new SoapConfig();

        /// <inheritdoc cref="SoapCommunicatorBuilder"/>
        /// <param name="host"><inheritdoc cref="IAppHost" path="/summary"/></param>
        public SoapCommunicatorBuilder(IAppHost host) : base(host)
        {
            _logger=LoggerManager.CreateLogger<SoapCommunicatorBuilder>();
        }

        /// <inheritdoc/>
        public ISoapCommunicatorBuilder ConfigureSoapConfiguration(Action<SoapConfig> configure)
        {
            Configuration.Bind(nameof(SoapConfig), _soapConfig);

            configure?.Invoke(_soapConfig);

            this.ConfigureServices((services) =>
            {
                services.AddSingleton(_soapConfig);
            });

            _logger.LogInformation($"完成{nameof(SoapConfig)}配置：{{@value}}", _soapConfig);

            return this;
        }
    }
}
